<template>
  <div class="home">
    <div class="header">
      <van-nav-bar fixed :class="{ active: headerScroll }">
        <template #left>
          <van-icon :class="{ active: headerScroll }" name="apps-o" />
        </template>
        <template #title>
          <div class="header-search">
            <span class="app-name">新蜂商城</span>
            <i class="iconfont icon-search"></i>
            <router-link tag="span" class="search-title" to="/search"
              >山河无恙，人间皆安</router-link
            >
          </div>
        </template>
        <template #right
          ><router-link
            class="login"
            :class="{ active: headerScroll }"
            tag="span"
            to="./login"
            v-if="!isLogin"
            >登录</router-link
          ></template
        >
      </van-nav-bar>
    </div>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#1baeae">
      <van-swipe-item v-for="(swiper, index) in swiperList" :key="index">
        <img class="swiper-img" v-lazy="swiper.carouselUrl"
      /></van-swipe-item>
    </van-swipe>
    <van-grid :column-num="5">
      <van-grid-item
        v-for="grid in categoryList"
        :key="grid.categoryId"
        :icon="grid.imgUrl"
        :text="grid.name"
      />
    </van-grid>
    <div class="good">
      <header class="good-header">新品上线</header>
      <div class="good-box">
        <div
          class="good-item"
          v-for="newgood in newGoodsList"
          :key="newgood.goodsId"
        >
          <img v-lazy="newgood.goodsCoverImg" />
          <div class="good-desc">
            <div class="title">{{ newgood.goodsName }}</div>
            <div class="price">￥{{ newgood.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="good">
      <header class="good-header">热门商品</header>
      <div class="good-box">
        <div
          class="good-item"
          v-for="hotgood in hotGoodsList"
          :key="hotgood.goodsId"
        >
          <img v-lazy="hotgood.goodsCoverImg" />
          <div class="good-desc">
            <div class="title">{{ hotgood.goodsName }}</div>
            <div class="price">￥{{ hotgood.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="good">
      <header class="good-header">最新推荐</header>
      <div class="good-box">
        <div
          class="good-item"
          v-for="recgood in recommendGoodsList"
          :key="recgood.goodsId"
        >
          <img v-lazy="recgood.goodsCoverImg" />
          <div class="good-desc">
            <div class="title">{{ recgood.goodsName }}</div>
            <div class="price">￥{{ recgood.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      value: '',
      isLogin: false,
      headerScroll: false,
      swiperList: [],
      categoryList: [
        {
          name: '新蜂超市',
          imgUrl: 'http://s.weituibao.com/1583585285461/cs.png',
          categoryId: 100001
        }, {
          name: '新蜂服饰',
          imgUrl: 'http://s.weituibao.com/1583585285468/fs.png',
          categoryId: 100003
        }, {
          name: '全球购',
          imgUrl: 'http://s.weituibao.com/1583585285470/qq.png',
          categoryId: 100002
        }, {
          name: '新蜂生鲜',
          imgUrl: 'http://s.weituibao.com/1583585285472/sx.png',
          categoryId: 100004
        }, {
          name: '新蜂到家',
          imgUrl: 'http://s.weituibao.com/1583585285467/dj.png',
          categoryId: 100005
        }, {
          name: '充值缴费',
          imgUrl: 'http://s.weituibao.com/1583585285465/cz.png',
          categoryId: 100006
        }, {
          name: '9.9元拼',
          imgUrl: 'http://s.weituibao.com/1583585285469/pt.png',
          categoryId: 100007
        }, {
          name: '领劵',
          imgUrl: 'http://s.weituibao.com/1583585285468/juan.png',
          categoryId: 100008
        }, {
          name: '省钱',
          imgUrl: 'http://s.weituibao.com/1583585285471/sq.png',
          categoryId: 100009
        }, {
          name: '全部',
          imgUrl: 'http://s.weituibao.com/1583585285470/qb.png',
          categoryId: 100010
        }
      ],
      newGoodsList: [],
      hotGoodsList: [],
      recommendGoodsList: []
    }
  },
  mounted() {
    window.addEventListener('scroll', this.pageScroll)
  },
  created() {
    this.getIndexInfo()
  },
  methods: {
    async getIndexInfo() {
      const res = await this.$http.getIndexInfo()
      this.swiperList = res.data.data.carousels
      this.newGoodsList = res.data.data.newGoodses
      this.hotGoodsList = res.data.data.hotGoodses
      this.hotGoodsList.forEach((item, index) => {
        if (index >= 1) {
          item.goodsCoverImg = 'http://backend-api-01.newbee.ltd' + item.goodsCoverImg
        }
      })
      this.recommendGoodsList = res.data.data.recommendGoodses
      this.recommendGoodsList.forEach((item, index) => {
        if (index !== 5) {
          item.goodsCoverImg = 'http://backend-api-01.newbee.ltd' + item.goodsCoverImg
        }
      })
    },
    pageScroll() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      scrollTop > 100 ? this.headerScroll = true : this.headerScroll = false
    }
  }
}
</script>
<style lang="scss">
.home {
  padding-top: 46px;
  padding-bottom: 50px;
  .header {
    background-color: transparent;
    position: relative;
    z-index: 999;
    .van-nav-bar {
      background-color: transparent;
      &.active {
        background-color: #1baeae;
      }
      .van-icon {
        font-size: 20px;
        color: #1baeae;
        &.active {
          color: #fff;
        }
      }
      .van-nav-bar__text {
        font-size: 16px;
        color: #1baeae;
      }
    }
    .header-search {
      width: 100%;
      display: flex;
      line-height: 20px;
      margin: 10px 0;
      padding: 5px 0;
      color: #232326;
      background: hsla(0, 0%, 100%, 0.7);
      border-radius: 20px;
      .app-name {
        padding: 0 10px;
        font-size: 20px;
        font-weight: bold;
        color: #1baeae;
      }
      .icon-search {
        font-size: 17px;
      }
      .search-title {
        font-size: 12px;
        color: #666;
        line-height: 21px;
      }
    }
    .login {
      color: #1baeae;
      line-height: 52px;
      &.active {
        color: #fff;
      }
      .van-icon-manager-o {
        font-size: 20px;
        vertical-align: -3px;
      }
    }
  }
  .my-swipe {
    position: relative;
    top: -46px;
    .swiper-img {
      width: 100%;
      height: 100%;
    }
  }
  .good {
    .good-header {
      background: #f9f9f9;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 16px;
      font-weight: 500;
      color: #1baeae;
    }
    .good-box {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      .good-item {
        box-sizing: border-box;
        width: 50%;
        border-bottom: 1px solid #e9e9e9;
        padding: 10px 10px;
        img {
          display: block;
          width: 120px;
          margin: 0 auto;
        }
        .good-desc {
          text-align: center;
          font-size: 14px;
          padding: 10px 0;
          .title {
            color: #222333;
          }
          .price {
            color: #1baeae;
          }
        }
        &:nth-child(2n + 1) {
          border-right: 1px solid #e9e9e9;
        }
      }
    }
  }
}
</style>